/*----------------------------------------------------------------------------------*/
/*!
 * @file utils.c
 * @brief Verschiedene kleine Funktionen
 * @author Steff
 *
 */
/*------------------------------------------------------------------------------------

REVISION CONTROL:
$Id: utils.c,v 1.4 2006/08/12 18:06:19 steffmeister Exp $
$ProjectName: $
$ProjectRevision: $

UPDATE:
$Log: utils.c,v $
Revision 1.4  2006/08/12 18:06:19  steffmeister
Optimierung, Anpassung der Kommentare

Revision 1.1  2006/08/12 19:43:41  steff
*** empty log message ***


------------------------------------------------------------------------------------*/

#include "utils.hxx"


/*----------------------------------------------------------------------------------*/
/*!
 * @fn char stringCopyN(char *string2, char *string1, int start, int end)
 *
 * @param string2 Pointer auf Zielvariable
 * @param string1 Pointer auf String, aus dem kopiert werden soll
 * @param start Startindex für das Kopieren
 * @param end Endindex für das Kopieren 
 *
 * @return Status, 0 bei Fehler, 1 bei Erfolg
 *
 * @brief string1 wird von Index start bis zum Index end in die Variable string2
 * kopiert.
 *
 */
/*----------------------------------------------------------------------------------*/
char stringCopyN(char *string2, char *string1, int start, int end)
{
	int counter = start;
	char ret = 0;
	int counter2 = 0;
	if (end > start)
	{
		while (counter <= end)
		{
			*(string2 + counter2) = *(string1 + counter);
			counter2++;
			counter++;
		}
		*(string2 + counter2) = '\0';
		ret = 1;
	} else
	{
		ret = 0;
	}
	return ret;
}

/*----------------------------------------------------------------------------------*/
/*!
 * @fn char removeNewlineFromString(char *string)
 *
 * @param string der zu untersuchende String
 *
 * @return gibt immer 1 zurück
 *
 * @brief entfernt alle Newlines ('\n') aus einem String
 *
 */
/*----------------------------------------------------------------------------------*/
char removeNewlineFromString(char *string)
{
	unsigned char stringLen = strlen(string);
	unsigned char counter = 0;
	unsigned char counter2 = 0;
	char newString[stringLen];
	while (counter2 < stringLen)
	{
		if (string[counter2] == '\n') counter2++;
		newString[counter] = string[counter2];
		counter++;
		counter2++;
	}
	strcpy(string, newString);
	return 1;
}

/*----------------------------------------------------------------------------------*/
/*!
 * @fn int getCleanExecPath(char *pathAndExec)
 *
 * @param pathAndExec Pointer auf Argument 0 (Pfad + Name des Befehls)
 *
 * @return gibt die Länge des bearbeiteten Strings zurück
 *
 * @brief gibt den vollständigen Namen des Arbeitsverzeichnis zurück
 *
 */
/*----------------------------------------------------------------------------------*/
int getCleanExecPath(char *pathAndExec)
{
	int length = strlen(pathAndExec);
	while ((length >= 0) && (*(pathAndExec + length) != '/') ) length--;
	*(pathAndExec + length + 1) = '\0';
	return length;
}

/*----------------------------------------------------------------------------------*/
/*!
 * @fn void waitForKey(SDLKey key)
 *
 * @param key die (SDL-) Bezeichnung der Taste, auf die gewartet werden soll
 *
 * @brief wartet bis eine Taste gedrückt worden ist
 *
 */
/*----------------------------------------------------------------------------------*/
void waitForKey(SDLKey key)
{
	char quit = 0;
	SDL_Event event;
	while (!quit)
	{
		if (SDL_PollEvent(&event) && (event.key.keysym.sym == key) && (event.type == SDL_KEYDOWN)) quit = 1;
		SDL_Delay(50);
	}
}

void fade_sdl_surface(SDL_Surface * target_surface, unsigned int degree)
{
/*    SDL_Surface * black_surface;
    black_surface = SDL_CreateRGBSurface(target_surface->flags, target_surface->w, target_surface->h, VIDEO_BITS, 0, 0, 0, degree);
    SDL_BlitSurface(black_surface, NULL, target_surface, NULL);*/
    boxRGBA(target_surface, 0, 0, target_surface->w, target_surface->h, 0, 0, 0, degree);
}

/*----------------------------------------------------------------------------------*/
/*!
 * @fn void clear_array(unsigned char * array, unsigned int length)
 *
 * @param array
 * @param length
 *
 * @brief resets every element of the array to zero.
 *
 */
/*----------------------------------------------------------------------------------*/
void clear_array(unsigned int * array, unsigned int length)
{
	unsigned int counter = 0;
	while (counter < length)
	{
		array[length] = 0;
	//	*(array + length) = 0;
		counter++;
	}
}

void endianSwap(unsigned short& x)
{
    x = (x>>8) | 
        (x<<8);
}

void endianSwap(unsigned int& x)
{
    x = (x>>24) | 
        ((x<<8) & 0x00FF0000) |
        ((x>>8) & 0x0000FF00) |
        (x<<24);
}

// __int64 for MSVC, "long long" for gcc
/*
inline void endianSwap(unsigned long& x)
{
    x = (x>>56) | 
        ((x<<40) & 0x00FF000000000000) |
        ((x<<24) & 0x0000FF0000000000) |
        ((x<<8)  & 0x000000FF00000000) |
        ((x>>8)  & 0x00000000FF000000) |
        ((x>>24) & 0x0000000000FF0000) |
        ((x>>40) & 0x000000000000FF00) |
        (x<<56);
}
*/
